<template>
  <standard-drawer
    ref="standardDrawer"
    :title="readOnly ? '详情' : (editType === 'update' ? '数据类型编辑' : '新增数据类型')"
    @close="close"
    @confirm="confirm"
    :confirmLoading="confirmLoading"
    :editable="!readOnly"
  >
    <a-form-model
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-model-item label="类型编码" prop="code">
        <a-input v-model="form.code" placeholder="请输入类型编码(字母数字组合)" :disabled="editType === 'update'"/>
      </a-form-model-item>
      <a-form-model-item label="类型名称" prop="name">
        <a-input v-model="form.name" placeholder="请输入类型名称"/>
      </a-form-model-item>
      <a-form-model-item label="字典分类" prop="dictType">
        <c-radio-group d-type="sys_dict_type" v-model="form.dictType"/>
      </a-form-model-item>
      <a-form-model-item label="可用状态" prop="useFlag">
        <c-radio-group d-type="sys_use_flag" v-model="form.useFlag"/>
      </a-form-model-item>
      <!--
      <a-form-model-item label="子项数据类型" prop="itemDataType">
        {{form.itemDataType}}
      </a-form-model-item>
      -->
      <a-form-model-item label="描述" prop="description">
        <a-input type="textarea" v-model="form.description" placeholder="请输入数据类型描述"/>
      </a-form-model-item>
    </a-form-model>
  </standard-drawer>
</template>

<script>
// import { axios } from '@/utils/request'
import { StandardDrawer } from '@/components'
import CRadioGroup from '@/views/common/RadioGroup'
// 字典类 编辑 面板
export default {
  name: 'DictEditPanel',
  components: { StandardDrawer, CRadioGroup },
  data () {
    return {
      confirmLoading: false,
      readOnly: false,
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      editType: 'add',
      form: {
        code: '',
        name: '',
        dictType: 'system',
        useFlag: '1',
        itemDataType: 'string'
      },
      rules: {
        code: [
          { required: true, message: '此项为必输项', trigger: 'blur' },
          { max: 50, message: '最多输入50个字符', trigger: 'change' }
        ],
        name: [
          { required: true, message: '此项为必输项', trigger: 'blur' },
          { max: 50, message: '最多输入50个字符', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    show (data, readOnly) {
      if (data) this.form = Object.assign({}, data) || {}
      this.editType = this.form.code ? 'update' : 'add'
      this.$refs.standardDrawer.show()
    },
    close () {
      this.form = {
        code: '',
        name: '',
        dictType: 'system',
        useFlag: '1',
        itemDataType: 'string'
      }
      this.confirmLoading = false
      this.$refs.ruleForm.resetFields()
    },
    confirm () {
      this.confirmLoading = true
      const req = this.editType === 'update' ? this.$http.put : this.$http.post
      const params = Object.assign({}, this.form)
      req('dict/type', params).then(res => {
        this.confirmLoading = false
        if (res.data) {
          this.$refs.standardDrawer.close()
          this.$message.success('操作成功')
          this.$emit('submit-success')
        } else {
          this.$message.error('操作失败')
        }
      }).catch(e => {
        this.confirmLoading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
